<?php
/**
 * User
 *
 * PHP version 5
 *
 * @category Controller
 * @version  1.0
 * @author   Duonght <hduongdmit@gmail.com>
 */
App::uses('AppController', 'Controller');
class MemberController extends AppController{

	/**
	 * Login admin
	 */
	public function login() {
		/* $sercuritySalt = Configure::read('Security.salt');
		//$password = $memberInfo->code . $loginPassword . $securitySalt;
		echo $pass = Security::hash('admin123456' . $sercuritySalt, 'sha256', true).'<br>';
		echo strlen($pass); */
		$this->layout = 'login';
		if ($this->Auth->user()) {
			return $this->redirect($this->Auth->redirect());
		}
		if ($this->request->is('post')) {
			$oldPassword = $this->request->data['Member']['password'];
			$this->request->data['Member']['password'] = $this->request->data['Member']['loginid'] . $oldPassword . Configure::read('Security.salt');

			if ($this->Auth->login()) {
				$this->Session->write('admin_login_success', 'admin_login_cuccess'.Configure::read('Security.salt'));
				return $this->redirect($this->Auth->redirect());
			} else {
				$this->request->data['Member']['password'] = $oldPassword;
			}

			$this->Session->setFlash(__('Please input correct username/password'), 'error');
		}

		$this->set('title_for_layout',__('Login administrator'));
	}

	/**
	 * Logout admin
	 */
	public function logout() {
		return $this->redirect($this->Auth->logout());
	}

	public function getProfile() {
		$this->render('profile');
	}

	public function changePassword(){
		if ($this->request->is('post')) {
			$valid = false;
			if ($this->Member->changePassValidate()) {
				if ($this->request->data['Member']['newPass'] !== $this->request->data['Member']['confirmPass']) {
					$this->Member->validationErrors['newPass'][] = 'New password and confirm password not same';
					$this->Member->validationErrors['confirmPass'][] = 'New password and confirm password not same';
				} else {
					$valid = true;
					$newPassword = Security::hash($this->Auth->user('loginid') . $this->request->data['Member']['newPass'] . Configure :: read('Security.salt'), 'sha256', true);
					$this->Member->read('id', $this->Auth->user('id'));
					$this->Member->set('password', $newPassword);
					$this->Member->save();
					$this->Session->setFlash(__('Change password success'), 'error');
					//$this->redirect(array('controller' => 'news', 'action' => 'index'));
				}
			}

			$this->set('valid', $valid);
		}

		$this->set('loginid', $this->Auth->user('loginid'));
		$this->set('title_for_layout', 'Change Password admin');
		$this->render('change_password');
	}
}